package cn.wes.waudiotool.file;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.wes.waudiotool.R;
import cn.wes.waudiotool.audio.AudioActivity;

public class FilesActivity extends AppCompatActivity {


    private Button bt_Choice;
    private Button bt_Exit;
    private Context context;
    private FindFile mfindFile;
    private ListView lvMusicFiles;
    private View musicFileList;
    private File[] files;
    private String checkedPath = null;

    private final static String TAG = "Find";
    private final String path="/sdcard";

    List<HashMap<String, Object>> listItem = new ArrayList<HashMap<String,Object>>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_files);
        context = this;
//        mfindFile = new FindFile();

        bt_Choice = findViewById(R.id.btChoice);
        bt_Exit = findViewById(R.id.btExit);

        musicFileList = View.inflate(this, R.layout.activity_files, null);
        lvMusicFiles = findViewById(R.id.lvMusic_Files);

        files = new File(path).listFiles();
        searchAllFile(files);
//        mfindFile.searchinit();

        bt_Choice.setOnClickListener(View -> {
            Intent intent = new Intent(FilesActivity.this, AudioActivity.class);
            intent.putExtra("checked", checkedPath);
            startActivity(intent);
        });

        bt_Exit.setOnClickListener(View -> {
            Intent intent = new Intent(FilesActivity.this, AudioActivity.class);
            startActivity(intent);

        });


        SimpleAdapter adapter =
                new SimpleAdapter(
                        this,
                        listItem,
                        R.layout.lv_music_files,
                        new String[]{"name"},
                        new int[]{R.id.music_name}
                );

        lvMusicFiles.setAdapter(adapter);
        lvMusicFiles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long aid) {
                String filePath = files[position].getAbsolutePath();
                HashMap<String, Object> result = listItem.get(position);
                checkedPath = (String) result.get("name");
                Toast.makeText(context, checkedPath, Toast.LENGTH_SHORT).show();

//                Intent intent = getWordFileIntent(filePath);
//                startActivity(intent);
            }
        });

    }

    public void searchAllFile(File[] files){
        for (File file : files)
        {
            if (file.isDirectory())//若为目录则递归查找
            {
                searchAllFile(file.listFiles());
            }
            else if (file.isFile())
            {
                String path = file.getPath();
                if (path.endsWith(".mp3") || path.endsWith(".wav"))//查找指定扩展名的文件
                {
                    //do someth
                    HashMap<String,Object> map;
                    map = new HashMap<String,Object>();
                    map.put("name", path);
                    Log.i(TAG, "path :" + path );
                    listItem.add(map);
                }
            }
        }
    }
}